package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.constant.HttpStatus;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.common.utils.StringUtils;
import com.crm.domain.Clue;
import com.crm.dto.CluePageQueryDto;
import com.crm.mapper.ClueMapper;
import com.crm.service.ClueService;
import org.springframework.stereotype.Service;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Override
    public PageResult pageClue(CluePageQueryDto dto) {
        Page<Clue> cluePage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<Clue> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getId()!=null,Clue::getId,dto.getId());
        wrapper.like(StringUtils.isNotEmpty(dto.getPhone()),Clue::getPhone,dto.getPhone());
        wrapper.like(StringUtils.isNotEmpty(dto.getOwner()),Clue::getOwner,dto.getOwner());
        wrapper.eq(dto.getStatus()!=null,Clue::getStatus,dto.getStatus());
        wrapper.eq(dto.getChannel()!=null,Clue::getChannel,dto.getChannel());
        wrapper.in(Clue::getLevel,Clue.CLUE_LEVEL_STRONG,Clue.CLUE_LEVEL_CONSIDERING,Clue.CLUE_LEVEL_WATCHING);
        if(dto.getParams().get("beginTime")!=null && dto.getParams().get("beginTime")!=""
        && dto.getParams().get("endTime")!=null && dto.getParams().get("endTime")!=""){
            wrapper.between(Clue::getCreateTime,dto.getParams().get("beginTime"),dto.getParams().get("endTime"));
        }
        Page<Clue> page = super.page(cluePage, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setMsg("查询成功");
        pageResult.setCode(200);
        return pageResult;
    }

    @Override
    public AjaxResult pageCluePool(CluePageQueryDto dto) {
        Page<Clue> cluePage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<Clue> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getId()!=null,Clue::getId,dto.getId());
        wrapper.like(StringUtils.isNotEmpty(dto.getPhone()),Clue::getPhone,dto.getPhone());
        wrapper.eq(dto.getChannel()!=null,Clue::getChannel,dto.getChannel());
        wrapper.eq(Clue::getLevel,Clue.CLUE_LEVEL_DEAD);
        if(dto.getParams().get("beginTime")!=null && dto.getParams().get("beginTime")!=""
                && dto.getParams().get("endTime")!=null && dto.getParams().get("endTime")!=""){
            wrapper.between(Clue::getCreateTime,dto.getParams().get("beginTime"),dto.getParams().get("endTime"));
        }
        Page<Clue> page = super.page(cluePage, wrapper);
        AjaxResult ajax = new AjaxResult();
        ajax.put("total", page.getTotal());
        ajax.put("rows", page.getRecords());
        ajax.put("code", HttpStatus.SUCCESS);
        return ajax;
    }
}
